<?php
namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Session;
use think\Request;

class User extends Admincontroller
{
    public function list()
    {
        //获取用户表信息
        // $list = Db::table('lab_admin')
        //         ->join('lab_user-role','lab_admin.id = lab_user-role.uid')
        //         ->join('lab_role','lab_user-role.rid = lab_role.id')
        //         ->select();
        $list = Db::name('admin_users')
                ->join('user_roles','admin_users.id = user_roles.user_id')
                ->join('roles','user_roles.role_id = roles.id')
                ->order('user_id','ASC')->select();     
        $rolelist = Db::name('roles')->field(true)->order('id','ASC')->select();
        return $this->fetch('user/list',[
            'list' => $list,
            'rolelist' => $rolelist
        ]);
    }

    public function doadd(Request $request)
    {
        $p=$request->post();
        if($_POST['password1'] !== $_POST['password']){
            return $this->error('两次密码不一致(ノ°ο°)ノ高能预警 ', url('/adminUserList'));
        }
        $data = [
            'name' => $p['name'],
            'username' => $p['username'],
            'password' => md5($p['password']),
            'sex' => $p['sex'],
            'email' => $p['email']
        ];
        $result = Db::name('admin_users')->data($data)->insert();
        if ($result < 0) {
            return $this->error('添加失败(ノ°ο°)ノ高能预警 ', url('/adminUserList'));
        }
        $userid = Db::name('admin_users')->getLastInsID();
        $data = [
            'user_id' => $userid,
            'role_id' =>$p['role']
        ];
        $result = Db::name('user_roles')->data($data)->insert();
        if ($result > 0) {
            return $this->success('添加成功', url('/adminUserList'));
        }else{
            return $this->error('角色添加失败(ノ°ο°)ノ高能预警 ', url('/adminUserList'));
        }
    }

    public function edit($id)
    {
        // 查询指定用户信息
        $row = Db::name('admin_users')->field('pass', true)->find($id);
        $list = Db::name('user_roles')->where('user_id',$id)->find();
        $rolelist = Db::name('roles')->field(true)->order('id','ASC')->select();
        return view('user/edit', [
            'data' => $row,
            'list' => $list,
            'rolelist' => $rolelist
        ]);
    }

    public function doedit($id)
    {
        // var_dump($_POST);
        // var_dump($id);
        // exit;
        $data = [
            'name' => $_POST['name'],
            'username' => $_POST['username'],
            'sex' => $_POST['sex'],
            'email' => $_POST['email']
        ];
        $result = Db::name('admin_users')->where('id',$id)->update($data);
        if ($result < 0) {
            return $this->error('编辑失败(ノ°ο°)ノ高能预警 ');
        } 
        $data = [
            'role_id' =>$_POST['role']
        ];
        $result = Db::name('user_roles')->where('user_id',$id)->update($data);
        if ($result<0) {
            return $this->error('角色修改失败(ノ°ο°)ノ高能预警 ', url('/adminUserList'));
        }else{
            return $this->success('修改成功', url('/adminUserList'));
        }

    }

    public function delete($id)
    {
        $result = Db::name('admin_users')->delete($id);
        if ($result) {
            $info['status'] = true;
            $info['id'] = $id;
            $info['info'] = 'ID为: ' . $id . '的用户删除成功!';
        } else {
            $info['status'] = false;
            $info['id'] = $id;
            $info['info'] = 'ID为: ' . $id . '的用户删除失败!';
        }

        return json($info);
    }
}
